﻿#region Copyright

// **********************************************************************
// Script Name :		EventMgr.cs
// Author Name :		Jerry
// Create Time :		2022-02-05 23:33:39
// Description :
// **********************************************************************

#endregion

using System.Collections.Generic;
using UnityEngine;

namespace Jerry.UiFrame
{
    /// <summary>
    ///     事件管理器
    /// </summary>
    public abstract class EventMgr
    {
        /// <summary>
        ///     带参数的
        /// </summary>
        /// <param name="p"></param>
        public delegate void Handler(params object[] p); //可以带参数的

        /// <summary>
        ///     不带参数的
        /// </summary>
        public delegate void HandlerNoParam();

        /// <summary>
        ///     带参数的
        /// </summary>
        private static readonly Dictionary<string, EventClass> eventNamesDictionary = new();

        private static readonly Dictionary<string, EventClass> eventNamesNoParam = new();

        public static void AddListener(string eventName, Handler function)
        {
            if (!eventNamesDictionary.ContainsKey(eventName))
            {
                var c = new EventClass();
                c.EventHandler += function;
                eventNamesDictionary.Add(eventName, c);
            }
            else
            {
                eventNamesDictionary[eventName].EventHandler += function;
            }
        }

        public static void AddListener(string eventName, HandlerNoParam function)
        {
            if (!eventNamesNoParam.ContainsKey(eventName))
            {
                var c = new EventClass();
                c.EventHandlerNoParam += function;
                eventNamesNoParam.Add(eventName, c);
            }
            else
            {
                eventNamesNoParam[eventName].EventHandlerNoParam += function;
            }
        }

        /// <summary>
        ///     需要通知各个订阅的函数，进行调用
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="p"></param>
        public static void Broadcast(string eventName, params object[] p)
        {
            if (!eventNamesDictionary.ContainsKey(eventName))
                Debug.LogError("未注册事件----->" + eventName);
            else
                eventNamesDictionary[eventName].ExecuteHandler(p);
        }

        /// <summary>
        ///     不带参数的事件
        /// </summary>
        /// <param name="eventName"></param>
        public static void Broadcast(string eventName)
        {
            if (!eventNamesNoParam.ContainsKey(eventName))
                Debug.LogError("未注册事件----->" + eventName);
            else
                eventNamesNoParam[eventName].ExecuteHandler();
        }

        /// <summary>
        ///     移除事件
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="function"></param>
        public static void RemoveListener(string eventName, Handler function)
        {
            if (!eventNamesDictionary.ContainsKey(eventName))
                Debug.LogError("未注册事件----->" + eventName);
            else
                eventNamesDictionary[eventName].EventHandler -= function;
        }

        /// <summary>
        ///     移除事件
        /// </summary>
        /// <param name="eventName"></param>
        /// <param name="function"></param>
        public static void RemoveListener(string eventName, HandlerNoParam function)
        {
            if (!eventNamesNoParam.ContainsKey(eventName))
                Debug.LogError("未注册事件----->" + eventName);
            else
                eventNamesNoParam[eventName].EventHandlerNoParam -= function;
        }

        private class EventClass
        {
            public event Handler EventHandler;
            public event HandlerNoParam EventHandlerNoParam;

            public void ExecuteHandler(params object[] p)
            {
                EventHandler?.Invoke(p);
            }

            public void ExecuteHandler()
            {
                EventHandlerNoParam?.Invoke();
            }
        }
    }
}